package snapshot;
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.awt.image.ColorModel; 
import java.awt.image.WritableRaster; 
import java.io.*; 
import java.net.HttpURLConnection;

import javax.imageio.*; 
import javax.swing.*;
public class SnapShotDemo extends JFrame {
	public SnapShotDemo(String url, File file) throws Exception { 
	    JEditorPane editorPane = new JEditorPane(); 
	    editorPane.setEditable(false); 
	    editorPane.setPage(url);
	    JScrollPane jsp = new JScrollPane(editorPane); 
	    getContentPane().add(jsp); 
	    this.setLocation(0, 0); 
//	    this.setVisible(true);
	     
	    Thread.sleep(5 * 1000);

	    setSize(10000, 10000);

	    pack(); 
	    // BufferedImage image = new BufferedImage(editorPane.getWidth(), 
	    // editorPane.getHeight(), BufferedImage.TYPE_INT_RGB); 
	    BufferedImage image = new BufferedImage(editorPane.getWidth(), editorPane.getHeight(), 
	        BufferedImage.TYPE_INT_RGB); 
	    Graphics2D graphics2D = image.createGraphics(); 
	    editorPane.paint(graphics2D); 
	     
	    BufferedImage image1 = resize(image, 600, 400);

	    ImageIO.write(image1, "jpg", file); 
	    dispose(); 
	  }

	  public static void main(String[] args) throws Exception { 
	    new SnapShotDemo("http://www.baidu.com", new File("d:/file.jpg")); 
	  }

	  public static BufferedImage resize(BufferedImage source, int targetW, int targetH) { 
	    int type = source.getType(); 
	    BufferedImage target = null; 
	    double sx = (double) targetW / source.getWidth(); 
	    double sy = (double) targetH / source.getHeight(); 
	    if (sx > sy) { 
	      sx = sy; 
	      targetW = (int) (sx * source.getWidth()); 
	      // } else { 
	      // sy = sx; 
	      // targetH = (int) (sy * source.getHeight()); 
	    } 
	    if (type == BufferedImage.TYPE_CUSTOM) { // handmade 
	      ColorModel cm = source.getColorModel(); 
	      WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); 
	      boolean alphaPremultiplied = cm.isAlphaPremultiplied(); 
	      target = new BufferedImage(cm, raster, alphaPremultiplied, null); 
	    } else 
	      target = new BufferedImage(targetW, targetH, type); 
	    Graphics2D g = target.createGraphics(); 
	    // smoother than exlax: 
	    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
	    g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); 
	    g.dispose(); 
	    return target; 
	  } 
}
